package com.ld.collegetask;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;

import com.google.gson.Gson;
import com.ld.collegetask.beans.BaseJsonBean;
import com.ld.collegetask.databinding.ActivityMemberRegisterUserBinding;
import com.ld.collegetask.login.LoginActivity;
import com.ld.collegetask.serverInterface.RequestSerives;
import com.ld.collegetask.utils.AppApplication;
import com.ld.collegetask.utils.ToastUtil;

import cn.jpush.im.android.api.JMessageClient;
import cn.jpush.im.api.BasicCallback;
import cn.qqtheme.framework.picker.DatePicker;
import cn.qqtheme.framework.picker.OptionPicker;
import me.leefeng.promptlibrary.PromptDialog;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.scalars.ScalarsConverterFactory;

public class MemberRegisterUserActivity extends BaseActivity {
    private ActivityMemberRegisterUserBinding binding;
    private PromptDialog promptDialog;
    private String realname;
    private String username;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_member_register_user);
        promptDialog = new PromptDialog(this);
        setTitleView(this, binding.ilTitle, true, true, false, false);
        setLoadingView(this, binding.ilLoading);
        setTitle("申请账号");
        username = getIntent().getStringExtra("username");
        findView();
    }

    @Override
    public void loading() {

    }

    public void findView() {
        binding.rlMemberRegisterUserSchool.setOnClickListener(this);
        binding.memberRegisterUserButton.setOnClickListener(this);
        binding.rlMemberRegisterUserGender.setOnClickListener(this);
        binding.rlMemberRegisterUserBirthday.setOnClickListener(this);
        binding.memberRegisterUserUsername.addTextChangedListener(new TextWatcher() {

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                // 输入的内容变化的监听
                if (s.toString().trim().length() > 0) {
                    binding.memberRegisterUserButton.setBackgroundColor(Color.parseColor("#40D15C"));
                    binding.memberRegisterUserButton.setEnabled(true);
                } else {
                    binding.memberRegisterUserButton.setBackgroundColor(Color.parseColor("#CCCCCC"));
                    binding.memberRegisterUserButton.setEnabled(false);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

    private void member_register_user(String realname, int gender, String birthday, final String password, Integer schoolId) {
        Retrofit retrofit = new Retrofit.Builder().baseUrl(AppApplication.server).addConverterFactory(ScalarsConverterFactory.create()).build();
        RequestSerives requestSerives = retrofit.create(RequestSerives.class);
        Call<String> call = requestSerives.member_register_user(username, password, schoolId,realname, gender, birthday);
        call.enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                Gson gson = new Gson();
                BaseJsonBean base = gson.fromJson(response.body().toString(), BaseJsonBean.class);
                if ("1".equals(base.getCode())) {
                    JMessageClient.register(username, password, new BasicCallback() {
                        @Override
                        public void gotResult(int i, String s) {
                            Log.i("im",i+"*****"+s);
                            if (i == 0){
                                startActivity(new Intent(mActivity, LoginActivity.class));
                                finish();
                                promptDialog.dismiss();
                            }else {
                                promptDialog.showWarn("注册失败");
                            }
                        }
                    });
                } else {
                    promptDialog.showLoading(base.getMsg());
                }
            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {
                promptDialog.dismiss();
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1000 && resultCode == 1001) {
            schoolId = data.getIntExtra("schoolId", -1);
            String sname = data.getStringExtra("sname");
            binding.memberRegisterUserSchool.setText(sname);
        }
    }

    private String password;
    private String gender;
    private int intGender = 1;
    private String birthday;
    private int schoolId = -1;

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.rl_member_register_user_school:
                Intent intent = new Intent();
                intent.setClass(MemberRegisterUserActivity.this, SchoolMemberListActivity.class);
                intent.putExtra("requestCode", "1000");
                startActivityForResult(intent, 1000);
                break;
            case R.id.member_register_user_button:
                realname = binding.memberRegisterUserUsername.getText().toString();
                password = binding.memberRegisterUserPasswrod.getText().toString();
                gender = binding.memberRegisterUserGender.getText().toString();
                birthday = binding.memberRegisterUserBirthday.getText().toString();
                if ("".equals(realname)) {
                    ToastUtil.showLong(mActivity, "请填写昵称");
                    break;
                }
                if ("".equals(password)) {
                    ToastUtil.showLong(mActivity, "请填写密码");
                    break;
                }
                if ("选择性别".equals(gender)) {
                    ToastUtil.showLong(mActivity, "请选择性别");
                    break;
                }
                if ("出生年月".equals(birthday)) {
                    ToastUtil.showLong(mActivity, "请填写出生年月");
                    break;
                }
                if (schoolId == -1) {
                    ToastUtil.showLong(mActivity, "请选择学校");
                    break;
                }
                if ("男".equals(gender)){
                    intGender = 1;
                }else {
                    intGender = 0;
                }
                promptDialog.showLoading("正在注册");
                member_register_user(realname, intGender, birthday, password, schoolId);
                break;
            case R.id.rl_member_register_user_gender:
                OptionPicker picker = new OptionPicker(this, new String[]{
                        "男", "女",
                });
                picker.setOffset(2);
                picker.setSelectedIndex(0);
                picker.setTextSize(17);
                picker.setOnOptionPickListener(new OptionPicker.OnOptionPickListener() {
                    @Override
                    public void onOptionPicked(String option) {
                        binding.memberRegisterUserGender.setText(option);
                    }
                });
                picker.show();
                break;
            case R.id.rl_member_register_user_birthday:
                DatePicker picker2 = new DatePicker(this, DatePicker.YEAR_MONTH_DAY);
                picker2.setRange(1949, 2099);//年份范围
                picker2.setOnDatePickListener(new DatePicker.OnYearMonthDayPickListener() {
                    @Override
                    public void onDatePicked(String year, String month, String day) {
                        binding.memberRegisterUserBirthday.setText(year + "-" + month + "-" + day);
                    }
                });
                picker2.show();
                break;
        }
    }
}
